package com.itheima.service.impl;

import com.itheima.domain.Record;
import com.itheima.domain.RecordSelective;
import com.itheima.mapper.RecordMapper;
import com.itheima.service.RecordService;
import com.itheima.util.PageHelperUtil;
import entity.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * @Author: auuuu4
 * @Date: 2024/07/10/9:47
 * @Description:
 */
@Service
public class RecordServiceImpl implements RecordService {
    @Autowired
    RecordMapper recordMapper;
    @Override
    public PageResult selectAllRecordsByPage(Integer pageNum, Integer pageSize) {
        List<Record> list = PageHelperUtil.selectList(pageNum,pageSize,recordMapper,"selectAllRecord");
        return new PageResult(recordMapper.selectRecordNum(),list);
    }

    @Override
    public PageResult selectRecordByBorrower(Integer pageNum, Integer pageSize, String name) {
        if(name == null)
            return new PageResult(0,null);
        List<Record> list = PageHelperUtil.selectList(pageNum,pageSize,recordMapper,"selectRecordsByBorrower",new Object[]{name});
        return new PageResult(recordMapper.selectRecordNumByName(name), list);
    }

    @Override
    public int insertRecord(Record record) {
        if(record == null)
            return 0;
        return recordMapper.insert(record);
    }

    private String likeStr(String str){
        return '%'+str+'%';
    }
    @Override
    public PageResult selectRecordsSelective(Integer pageNum, Integer pageSize, RecordSelective selective) {
        if(selective == null)
            return new PageResult(0,null);
        String s = selective.getBorrower();
        if(s != null)
            selective.setBorrower(likeStr(s));
        s = selective.getBookname();
        if(s != null)
            selective.setBookname(likeStr(s));
        List<Record> list = PageHelperUtil.selectList(pageNum, pageSize,recordMapper,"selectRecordsSelective",new Object[]{selective});
        return new PageResult(list.size(),list);
    }
}
